package com.soyoung.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: zhuhongqiang
 * @since : 2018/5/15.
 * 过滤器
 */
public class Filter extends ZuulFilter {
    /** logger */
    private static final Logger log = LoggerFactory.getLogger(Filter.class);
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    //true:开启过滤器
    //false:关闭
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String accessToken = String.valueOf(request.getParameter("Token"));
        if(!accessToken.equals("zz")) {
            log.warn("access token is error");
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(401);
            currentContext.setResponseBody("{\"result\":\"accessToken is error!\"}");
            return null;
        }
        log.info("access token ok");
        return null;
    }
}
